/*
 * Created on 22.10.2003
 * Local Projects Database
 * (c) 2003 by eRomania Gateway Association
 */
package ro.gateway.aida.struts.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.LanguageDB;
import ro.gateway.aida.obj.db.SystemPropertyDB;
import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.struts.form.SystemPropertiesForm;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserGroupDB;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.LocalisedList;
import ro.gateway.aida.utils.SysProp;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 * e-mail: mihai@ro-gateway.org
 */
public class SystemPropertiesAction extends TilesAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.tokenLookup(sc);
            SystemPropertiesForm spf = (SystemPropertiesForm) form;
            Arbiter ar = Arbiter.getManager(token);
            SystemPropertyDB spdb = SystemPropertyDB.getManager(token);
            UserGroupDB ugdb = UserGroupDB.getManager(token);
            HttpSession session = request.getSession();
            User user = UserUtils.getLogged(request, response);
            String lang = TranslatorUtils.getCurrentLanguage(request, sc)
                                         .getLanguage();
            LocalisedList langs = (LocalisedList) Utils.getLocalisedSessionObj(session,
                    "Langs", lang);

            if (langs == null) {
                langs = LanguageDB.getManager(token).getAll();
                session.setAttribute("Langs", langs);
            }

            if (user == null)
                return mapping.findForward("plslogin");

            if (!ar.canDo(request, Perms.ADMIN_TOOLS)) {
                session.invalidate();

                return mapping.findForward("norights");
            }


            if (request.getParameter("update") != null) {
                spdb.setProperty(SysProp.DEFAULT_GROUP, spf.getDefaultGroup());
                spdb.setProperty(SysProp.MAIL_SERVER, spf.getMailServer());
                spdb.setProperty(SysProp.REPLY_TO_USER, spf.getReplyToUser());
                spdb.setProperty(SysProp.LIST_SIZE, spf.getListSize());
                spdb.setProperty(SysProp.DEFAULT_LANG, spf.getDefaultLang());
				spdb.setProperty(SysProp.SESSION_TIMEOUT, spf.getSessionTimeout());
                spdb.setProperty(SysProp.REGISTER_AUTO_LOGIN,spf.isRegisterAutoLogin()?"true":"false");
                
                spdb.setProperty(SysProp.FUNDING_ROLE,spf.getFundingRole());
                spdb.setProperty(SysProp.IMPLEMENTING_ROLE, spf.getImplementingRole());
                spdb.setProperty(SysProp.EXECUTING_ROLE, spf.getExecutingRole());

                //refresh system options
                token.setProperty("sysProps", spdb.getAll());
                Constants.DEFAULT_LANGUAGE=spf.getDefaultLang();
                LanguageDB.getManager(token).setDefault(spf.getDefaultLang());
            } else {
                String defaultGroup = spdb.getProperty(SysProp.DEFAULT_GROUP);
                String mailServer = spdb.getProperty(SysProp.MAIL_SERVER);
                String replyToUser = spdb.getProperty(SysProp.REPLY_TO_USER);
                String listSize = spdb.getProperty(SysProp.LIST_SIZE);
                String defaultLang = spdb.getProperty(SysProp.DEFAULT_LANG);
				String sessionTimout = spdb.getProperty(SysProp.SESSION_TIMEOUT);
				String registerAutoLogin = spdb.getProperty(SysProp.REGISTER_AUTO_LOGIN);
				
				String fundingRole = spdb.getProperty(SysProp.FUNDING_ROLE);
				String implementingRole = spdb.getProperty(SysProp.IMPLEMENTING_ROLE);
				String executingRole = spdb.getProperty(SysProp.EXECUTING_ROLE);
				
				
				
                spf.setDefaultGroup(Integer.parseInt(defaultGroup));
                spf.setMailServer(mailServer);
                spf.setReplyToUser(replyToUser);
                spf.setDefaultLang(defaultLang);
                spf.setListSize(Integer.parseInt(listSize));
                spf.setSessionTimeout(Integer.parseInt(sessionTimout));
                spf.setRegisterAutoLogin("true".equals(registerAutoLogin)?true:false);
                spf.setFundingRole(Integer.parseInt(fundingRole));
                spf.setImplementingRole(Integer.parseInt(implementingRole));
                spf.setExecutingRole(Integer.parseInt(executingRole));
                request.setAttribute("groups", ugdb.createGroupList());
                Utils.setBreadCrumbs(request, "bc.props");

                return mapping.findForward("edit");
            }

            ActionForward f = new ActionForward("/admin.jsp");
            f.setContextRelative(true);
            f.setRedirect(true);

            return f;
        } catch (Exception e) {
            BugTracker.publishBug(e, request);

            return mapping.findForward("fail");
        }
    }

    
}
